home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / gpt32src.zip / GRAPH3D.C < prev    next >
C/C++ Source or Header  |  1992-03-25  |  67KB  |  2,334 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: graph3d.c,v 3.26 92/03/24 22:34:27 woo Exp Locker: woo $";
  3. #endif
  4.  
  5. /* GNUPLOT - graph3d.c */
  6. /*
  7.  * Copyright (C) 1986, 1987, 1990, 1991, 1992   Thomas Williams, Colin Kelley
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted, 
  11.  * provided that the above copyright notice appear in all copies and 
  12.  * that both that copyright notice and this permission notice appear 
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the modified code.  Modifications are to be distributed 
  17.  * as patches to released version.
  18.  *
  19.  * This software is provided "as is" without express or implied warranty.
  20.  *
  21.  *
  22.  * AUTHORS
  23.  *
  24.  *   Original Software:
  25.  *       Gershon Elber and many others.
  26.  *
  27.  * Send your comments or suggestions to 
  28.  *  info-gnuplot@ames.arc.nasa.gov.
  29.  * This is a mailing list; to join it send a note to 
  30.  *  info-gnuplot-request@ames.arc.nasa.gov.  
  31.  * Send bug reports to
  32.  *  bug-gnuplot@ames.arc.nasa.gov.
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <math.h>
  37. #include <assert.h>
  38. #include <time.h>
  39. #include "plot.h"
  40. #include "setshow.h"
  41.  
  42. extern char *strcpy(),*strncpy(),*strcat(),*ctime(),*tdate;
  43. #ifdef AMIGA_AC_5
  44. extern time_t dated;
  45. #else
  46. #ifdef VMS
  47. extern time_t dated,time();
  48. #else
  49. extern long dated,time();
  50. #endif
  51. #endif
  52.  
  53. /*
  54.  * hidden_line_type_above, hidden_line_type_below - controls type of lines
  55.  *   for above and below parts of the surface.
  56.  * hidden_no_update - if TRUE lines will be hidden line removed but they
  57.  *   are not assumed to be part of the surface (i.e. grid) and therefore
  58.  *   do not influence the hidings.
  59.  * hidden_max_index - length of hidden_low_bound and hidden_high_bound vecs.
  60.  * hidden_first_row - TRUE if we are now drawing the first row of the surface.
  61.  * hidden_active - TRUE if hidden lines are to be removed.
  62.  * hidden_low_bound, hidden_high_bound - two vectors of size hidden_max_index
  63.  *   that hold the above and below floating horisons.
  64.  */
  65. static int hidden_line_type_above, hidden_line_type_below, hidden_no_update;
  66. static int hidden_max_index, hidden_first_row, hidden_active = FALSE;
  67. static int *hidden_low_bound, *hidden_high_bound;
  68. #define HIDDEN_BOUND(x)        (x < 0 ? 0 : x >= hidden_max_index ? \
  69.                              hidden_max_index - 1 : x)
  70. #define HIDDEN_LOW_BOUND(x) (hidden_low_bound[HIDDEN_BOUND(x)])
  71. #define HIDDEN_HIGH_BOUND(x) (hidden_high_bound[HIDDEN_BOUND(x)])
  72. #define SET_HIDDEN_BOUND(vec, x, y) (vec[HIDDEN_BOUND(x)] = y)
  73.  
  74. static plot3d_impulses();
  75. static plot3d_lines();
  76. static plot3d_points();
  77. static plot3d_dots();
  78. static cntr3d_impulses();
  79. static cntr3d_lines();
  80. static cntr3d_points();
  81. static cntr3d_dots();
  82. static update_extrema_pts();
  83. static draw_parametric_grid();
  84. static draw_non_param_grid();
  85. static draw_bottom_grid();
  86. static draw_3dxtics();
  87. static draw_3dytics();
  88. static draw_3dztics();
  89. static draw_series_3dxtics();
  90. static draw_series_3dytics();
  91. static draw_series_3dztics();
  92. static draw_set_3dxtics();
  93. static draw_set_3dytics();
  94. static draw_set_3dztics();
  95. static xtick();
  96. static ytick();
  97. static ztick();
  98. static setlinestyle();
  99.  
  100. #ifndef max        /* Lattice C has max() in math.h, but shouldn't! */
  101. #define max(a,b) ((a > b) ? a : b)
  102. #endif
  103.  
  104. #ifndef min
  105. #define min(a,b) ((a < b) ? a : b)
  106. #endif
  107.  
  108. #define inrange(z,min,max) ((min<max) ? ((z>=min)&&(z<=max)) : ((z>=max)&&(z<=min)) )
  109.  
  110. #define apx_eq(x,y) (fabs(x-y) < 0.001)
  111. #define abs(x) ((x) >= 0 ? (x) : -(x))
  112. #define sqr(x) ((x) * (x))
  113.  
  114. /* Define the boundary of the plot
  115.  * These are computed at each call to do_plot, and are constant over
  116.  * the period of one do_plot. They actually only change when the term
  117.  * type changes and when the 'set size' factors change. 
  118.  */
  119. static int xleft, xright, ybot, ytop, xmiddle, ymiddle, xscaler, yscaler;
  120.  
  121. /* Boundary and scale factors, in user coordinates */
  122. /* x_min3d, x_max3d, y_min3d, y_max3d, z_min3d, z_max3d are local to this
  123.  * file and are not the same as variables of the same names in other files
  124.  */
  125. static double x_min3d, x_max3d, y_min3d, y_max3d, z_min3d, z_max3d;
  126. static double xscale3d, yscale3d, zscale3d;
  127. static double real_z_min3d, real_z_max3d;
  128. static double min_sy_ox,min_sy_oy; /* obj. coords. for xy tics placement. */
  129. static double min_sx_ox,min_sx_oy; /* obj. coords. for z tics placement. */
  130.  
  131. typedef double transform_matrix[4][4];
  132. static transform_matrix trans_mat;
  133.  
  134. /* (DFK) Watch for cancellation error near zero on axes labels */
  135. #define SIGNIF (0.01)        /* less than one hundredth of a tic mark */
  136. #define CheckZero(x,tic) (fabs(x) < ((tic) * SIGNIF) ? 0.0 : (x))
  137. #define NearlyEqual(x,y,tic) (fabs((x)-(y)) < ((tic) * SIGNIF))
  138.  
  139. /* And the functions to map from user to terminal coordinates */
  140. #define map_x(x) (int)(x+0.5) /* maps floating point x to screen */ 
  141. #define map_y(y) (int)(y+0.5)    /* same for y */
  142.  
  143. /* And the functions to map from user 3D space into normalized -1..1 */
  144. #define map_x3d(x) ((x-x_min3d)*xscale3d-1.0)
  145. #define map_y3d(y) ((y-y_min3d)*yscale3d-1.0)
  146. #define map_z3d(z) ((z-z_min3d)*zscale3d-1.0)
  147.  
  148. static mat_unit(mat)
  149. transform_matrix mat;
  150. {
  151.     int i, j;
  152.  
  153.     for (i = 0; i < 4; i++) for (j = 0; j < 4; j++)
  154.     if (i == j)
  155.         mat[i][j] = 1.0;
  156.     else
  157.         mat[i][j] = 0.0;
  158. }
  159.  
  160. static mat_trans(tx, ty, tz, mat)
  161. double tx, ty, tz;
  162. transform_matrix mat;
  163. {
  164.      mat_unit(mat);                                 /* Make it unit matrix. */
  165.      mat[3][0] = tx;
  166.      mat[3][1] = ty;
  167.      mat[3][2] = tz;
  168. }
  169.  
  170. static mat_scale(sx, sy, sz, mat)
  171. double sx, sy, sz;
  172. transform_matrix mat;
  173. {
  174.      mat_unit(mat);                                 /* Make it unit matrix. */
  175.      mat[0][0] = sx;
  176.      mat[1][1] = sy;
  177.      mat[2][2] = sz;
  178. }
  179.  
  180. static mat_rot_x(teta, mat)
  181. double teta;
  182. transform_matrix mat;
  183. {
  184.     double cos_teta, sin_teta;
  185.  
  186.     teta *= Pi / 180.0;
  187.     cos_teta = cos(teta);
  188.     sin_teta = sin(teta);
  189.  
  190.     mat_unit(mat);                                  /* Make it unit matrix. */
  191.     mat[1][1] = cos_teta;
  192.     mat[1][2] = -sin_teta;
  193.     mat[2][1] = sin_teta;
  194.     mat[2][2] = cos_teta;
  195. }
  196.  
  197. static mat_rot_y(teta, mat)
  198. double teta;
  199. transform_matrix mat;
  200. {
  201.     double cos_teta, sin_teta;
  202.  
  203.     teta *= Pi / 180.0;
  204.     cos_teta = cos(teta);
  205.     sin_teta = sin(teta);
  206.  
  207.     mat_unit(mat);                                  /* Make it unit matrix. */
  208.     mat[0][0] = cos_teta;
  209.     mat[0][2] = -sin_teta;
  210.     mat[2][0] = sin_teta;
  211.     mat[2][2] = cos_teta;
  212. }
  213.  
  214. static mat_rot_z(teta, mat)
  215. double teta;
  216. transform_matrix mat;
  217. {
  218.     double cos_teta, sin_teta;
  219.  
  220.     teta *= Pi / 180.0;
  221.     cos_teta = cos(teta);
  222.     sin_teta = sin(teta);
  223.  
  224.     mat_unit(mat);                                  /* Make it unit matrix. */
  225.     mat[0][0] = cos_teta;
  226.     mat[0][1] = -sin_teta;
  227.     mat[1][0] = sin_teta;
  228.     mat[1][1] = cos_teta;
  229. }
  230.  
  231. /* Multiply two transform_matrix. Result can be one of two operands. */
  232. void mat_mult(mat_res, mat1, mat2)
  233. transform_matrix mat_res, mat1, mat2;
  234. {
  235.     int i, j, k;
  236.     transform_matrix mat_res_temp;
  237.  
  238.     for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) {
  239.         mat_res_temp[i][j] = 0;
  240.         for (k = 0; k < 4; k++) mat_res_temp[i][j] += mat1[i][k] * mat2[k][j];
  241.     }
  242.     for (i = 0; i < 4; i++) for (j = 0; j < 4; j++)
  243.     mat_res[i][j] = mat_res_temp[i][j];
  244. }
  245.  
  246. /* And the functions to map from user 3D space to terminal coordinates */
  247. static int map3d_xy(x, y, z, xt, yt)
  248. double x, y, z;
  249. int *xt, *yt;
  250. {
  251.     int i,j;
  252.     double v[4], res[4],             /* Homogeneous coords. vectors. */
  253.     w = trans_mat[3][3];
  254.  
  255.     v[0] = map_x3d(x); /* Normalize object space to -1..1 */
  256.     v[1] = map_y3d(y);
  257.     v[2] = map_z3d(z);
  258.     v[3] = 1.0;
  259.  
  260.     for (i = 0; i < 2; i++) {                 /* Dont use the third axes (z). */
  261.         res[i] = trans_mat[3][i];     /* Initiate it with the weight factor. */
  262.         for (j = 0; j < 3; j++) res[i] += v[j] * trans_mat[j][i];
  263.     }
  264.  
  265.     for (i = 0; i < 3; i++) w += v[i] * trans_mat[i][3];
  266.     if (w == 0) w = 1e-5;
  267.  
  268.     *xt = ((int) (res[0] * xscaler / w)) + xmiddle;
  269.     *yt = ((int) (res[1] * yscaler / w)) + ymiddle;
  270. }
  271.  
  272. /* And the functions to map from user 3D space to terminal z coordinate */
  273. static double map3d_z(x, y, z)
  274. double x, y, z;
  275. {
  276.     int i;
  277.     double v[4], res,                 /* Homogeneous coords. vectors. */
  278.     w = trans_mat[3][3];
  279.  
  280.     v[0] = map_x3d(x); /* Normalize object space to -1..1 */
  281.     v[1] = map_y3d(y);
  282.     v[2] = map_z3d(z);
  283.     v[3] = 1.0;
  284.  
  285.     res = trans_mat[3][2];               /* Initiate it with the weight factor. */
  286.     for (i = 0; i < 3; i++) res += v[i] * trans_mat[i][2];
  287.  
  288.     return res;
  289. }
  290.  
  291. /* Initialize the line style using the current device and set hidden styles  */
  292. /* to it as well if hidden line removal is enabled.                 */
  293. static setlinestyle(style)
  294. int style;
  295. {
  296.     register struct termentry *t = &term_tbl[term];
  297.  
  298.     (*t->linetype)(style);
  299.     if (hidden3d) {
  300.     hidden_line_type_above = style;
  301.     hidden_line_type_below = style;
  302.     }
  303. }
  304.  
  305. /* Initialize the necessary steps for hidden line removal. This algorithm    */
  306. /* is based on the "floating horizon" explicit surfaces hidden line removal. */
  307. static void init_hidden_line_removal()
  308. {
  309.     hidden_max_index = xright - xleft + 1;
  310.     hidden_low_bound = (int *) alloc(sizeof(int) * hidden_max_index, "hidden");
  311.     hidden_high_bound = (int *) alloc(sizeof(int) * hidden_max_index, "hidden");
  312. }
  313.  
  314. /* Reset the hidden line data to a fresh start.                     */
  315. static void reset_hidden_line_removal()
  316. {
  317.     int i;
  318.  
  319.     for (i = 0; i < hidden_max_index; i++) {
  320.         hidden_low_bound[i] = ytop;
  321.         hidden_high_bound[i] = ybot;
  322.     }
  323. }
  324.  
  325. /* Terminates the hidden line removal process. Free any memory allocated by  */
  326. /* init_hidden_line_removal above.                         */
  327. static void term_hidden_line_removal()
  328. {
  329.     free(hidden_low_bound);
  330.     free(hidden_high_bound);
  331. }
  332.  
  333. /* Given a list of parallel iso_curves, make sure the first one is closest   */
  334. /* to viewer or reverse in place the list otherwise.                 */
  335. /* Returns a pointer to the new ordered list.                     */
  336. static struct iso_curve *reorder_hidden_one_iso_list(orig_list)
  337. struct iso_curve *orig_list;
  338. {
  339.     double first_crv_first_ptz, first_crv_last_ptz,
  340.            last_crv_first_ptz, last_crv_last_ptz;
  341.     struct iso_curve *first_crv = orig_list, *last_crv;
  342.  
  343.     for (last_crv = first_crv; last_crv->next; last_crv = last_crv->next);
  344.  
  345.     first_crv_first_ptz = map3d_z(first_crv->points[0].x,
  346.                       first_crv->points[0].y,
  347.                       0.0);
  348.     first_crv_last_ptz = map3d_z(first_crv->points[first_crv->p_count-1].x,
  349.                      first_crv->points[first_crv->p_count-1].y,
  350.                      0.0);
  351.  
  352.     last_crv_first_ptz = map3d_z(last_crv->points[0].x,
  353.                      last_crv->points[0].y,
  354.                      0.0);
  355.     last_crv_last_ptz = map3d_z(last_crv->points[last_crv->p_count-1].x,
  356.                     last_crv->points[last_crv->p_count-1].y,
  357.                     0.0);
  358.  
  359.     /* If first curve is in front of last - do nothing. */
  360.     if ((first_crv_first_ptz > last_crv_first_ptz &&
  361.          first_crv_first_ptz > last_crv_last_ptz) ||
  362.         (first_crv_last_ptz > last_crv_first_ptz &&
  363.          first_crv_last_ptz > last_crv_last_ptz))
  364.         return orig_list;
  365.     else {
  366.         struct iso_curve *icrv1, *icrv2, *icrv3;
  367.  
  368.         /* Need to reverse the list order: */
  369.         for (icrv1 = orig_list, icrv2 = icrv1->next; icrv2 != NULL; ) {
  370.         icrv3 = icrv2->next;
  371.         icrv2->next = icrv1;
  372.         icrv1 = icrv2;
  373.             icrv2 = icrv3;
  374.         }
  375.  
  376.         orig_list->next = NULL; /* Now it is the last element. */
  377.  
  378.         return icrv1;
  379.     }
  380. }
  381.  
  382. /* Reorder the list of iso_curves in the surface plot, so it will be drawn   */
  383. /* from near iso curve to far one in both direction.                 */
  384. /* Returned is a pointer to new order iso curve list.                 */
  385. static struct iso_curve *reorder_hidden_iso_curves(plot, num_iso_lines)
  386.     struct surface_points *plot;
  387.     int num_iso_lines;
  388. {
  389.     int i;
  390.     struct iso_curve *iso_list1 = plot->iso_crvs, *itmp = iso_list1, *iso_list2;
  391.  
  392.     for (i = 1; i < num_iso_lines; i++) itmp = itmp->next;
  393.     iso_list2 = itmp->next;
  394.     itmp->next = NULL;
  395.  
  396.     iso_list1 = reorder_hidden_one_iso_list(iso_list1);
  397.     iso_list2 = reorder_hidden_one_iso_list(iso_list2);
  398.     for (itmp = iso_list1; itmp->next; itmp = itmp->next);
  399.     itmp->next = iso_list2;
  400.  
  401.     plot->iso_crvs = iso_list1;
  402.  
  403.     return iso_list1;
  404. }
  405.  
  406. /* Plot a line after removing is hidden part(s). */
  407. static void hidden_line_plot_aux(x1, y1, x2, y2, style)
  408. int x1, y1, x2, y2, style;
  409. {
  410.     register struct termentry *t = &term_tbl[term];
  411.  
  412.     if (x2 < x1 || (x2 == x1 && y1 == y2)) return;
  413.  
  414.     (*t->linetype)(style);
  415.     (*t->move)(x1,y1);
  416.     (*t->vector)(x2,y2);
  417. }
  418.  
  419. /* Plot a line after removing is hidden part(s). */
  420. static void hidden_line_plot(x1, y1, x2, y2)
  421. int x1, y1, x2, y2;
  422. {
  423.     int x, last_x, last_y, visible_above, visible_below;
  424.     float dy, y;
  425.  
  426.     if (x1 > x2) {
  427.     x = x1;
  428.     x1 = x2;
  429.     x2 = x;
  430.  
  431.     y = y1;
  432.     y1 = y2;
  433.     y2 = y;
  434.     }
  435.  
  436.     if (hidden_first_row) {
  437.     /* All is visible and drawn as visible above. */
  438.     dy = x1 != x2 ? ((float) (y2 - y1)) / ((float) (x2 - x1)) : VERYLARGE;
  439.  
  440.     for (x = x1, y = y1; x <= x2; x++, y = y1 + (x - x1) * dy) {
  441.         SET_HIDDEN_BOUND(hidden_high_bound, x, (int) y);
  442.         SET_HIDDEN_BOUND(hidden_low_bound, x, (int) y);
  443.     }
  444.         hidden_line_plot_aux(x1, y1, x2, y2, hidden_line_type_above);
  445.     }
  446.     else {
  447.     if (x1 == x2) {
  448.         if (y1 > y2) {
  449.         y = y1;
  450.         y1 = y2;
  451.         y2 = y;
  452.         }
  453.  
  454.         /* Clip properly this verical line to right bounds. Note clipping
  455.          * may produce upto two segments.
  456.          */
  457.         if (y1 <= HIDDEN_LOW_BOUND(x1) && y2 <= HIDDEN_LOW_BOUND(x1))
  458.         hidden_line_plot_aux(x1, y1, x2, y2, hidden_line_type_below);
  459.         else if (y1 >= HIDDEN_HIGH_BOUND(x1) && y2 >= HIDDEN_HIGH_BOUND(x1))  
  460.         hidden_line_plot_aux(x1, y1, x2, y2, hidden_line_type_above);
  461.         else {
  462.             if (y1 < HIDDEN_LOW_BOUND(x1) && y2 > HIDDEN_LOW_BOUND(x1))
  463.             hidden_line_plot_aux(x1, y1, x2, HIDDEN_LOW_BOUND(x1),
  464.                          hidden_line_type_below);
  465.             if (y2 > HIDDEN_HIGH_BOUND(x1) && y1 < HIDDEN_HIGH_BOUND(x1))
  466.             hidden_line_plot_aux(x1, HIDDEN_HIGH_BOUND(x1), x2, y2,
  467.                          hidden_line_type_above);
  468.         }
  469.         return;
  470.     }
  471.     else
  472.         dy = ((float) (y2 - y1)) / ((float) (x2 - x1));
  473.  
  474.     visible_above = y1 >= HIDDEN_HIGH_BOUND(x1);
  475.     visible_below = y1 <= HIDDEN_LOW_BOUND(x1);
  476.     if (visible_above || visible_below) {
  477.         if (visible_above && visible_below) visible_below = FALSE;
  478.         last_x = x1;
  479.         last_y = y1;
  480.     }
  481.         for (x = x1, y = y1; x < x2; x++, y += dy)
  482.     {
  483.         if (y >= HIDDEN_HIGH_BOUND(x)) {
  484.         if (visible_below) {
  485.             hidden_line_plot_aux(last_x, last_y, x - 1, (int) (y - dy),
  486.                      hidden_line_type_below);
  487.             visible_below = FALSE;
  488.         }
  489.         if (!visible_above) {
  490.             visible_above = TRUE;
  491.             last_x = x;
  492.             last_y = (int) y;
  493.         }
  494.         if (!hidden_no_update)
  495.             SET_HIDDEN_BOUND(hidden_high_bound, x, (int) y);
  496.         }
  497.         else if (y <= HIDDEN_LOW_BOUND(x)) {
  498.         if (visible_above) {
  499.             hidden_line_plot_aux(last_x, last_y, x - 1, (int) (y - dy),
  500.                      hidden_line_type_above);
  501.             visible_above = FALSE;
  502.  
  503.         }
  504.         if (!visible_below) {
  505.             visible_below = TRUE;
  506.             last_x = x;
  507.             last_y = (int) y;
  508.         }
  509.         if (!hidden_no_update)
  510.             SET_HIDDEN_BOUND(hidden_low_bound, x, (int) y);
  511.         }
  512.         else {
  513.         if (visible_above) {
  514.             hidden_line_plot_aux(last_x, last_y, x - 1, (int) (y - dy),
  515.                      hidden_line_type_above);
  516.             visible_above = visible_below = FALSE;
  517.         }
  518.         else if (visible_below) {
  519.             hidden_line_plot_aux(last_x, last_y, x - 1, (int) (y - dy),
  520.                      hidden_line_type_below);
  521.             visible_below = FALSE;
  522.         }
  523.         }
  524.     }
  525.  
  526.     if (visible_above) {
  527.         hidden_line_plot_aux(last_x, last_y, x2, y2,
  528.                  hidden_line_type_above);
  529.     }
  530.     else if (visible_below) {
  531.         hidden_line_plot_aux(last_x, last_y, x2, y2,
  532.                  hidden_line_type_below);
  533.     }
  534.     }
  535. }
  536.  
  537. /* Test a single point to be within the xleft,xright,ybot,ytop bbox.
  538.  * Sets the returned integers 4 l.s.b. as follows:
  539.  * bit 0 if to the left of xleft.
  540.  * bit 1 if to the right of xright.
  541.  * bit 2 if above of ytop.
  542.  * bit 3 if below of ybot.
  543.  * 0 is returned if inside.
  544.  */
  545. static int clip_point_for_clip_line(x, y)
  546. int x, y;
  547. {
  548.     int ret_val = 0;
  549.  
  550.     if (x < xleft) ret_val |= 0x01;
  551.     if (x > xright) ret_val |= 0x02;
  552.     if (y < ybot) ret_val |= 0x04;
  553.     if (y > ytop) ret_val |= 0x08;
  554.  
  555.     return ret_val;
  556. }
  557.  
  558. /* Test a single point to be within the xleft,xright,ybot,ytop bbox.
  559.  * and it is not hidden if hidden lines are to be removed.
  560.  */
  561. static int clip_point(x, y)
  562. int x, y;
  563. {
  564.     if (hidden_active &&
  565.     HIDDEN_LOW_BOUND(x) <= y && HIDDEN_HIGH_BOUND(x) >= y)
  566.     return FALSE;
  567.  
  568.     return clip_point_for_clip_line(x, y);
  569. }
  570.  
  571. /* Clip the given line to drawing coords defined as xleft,xright,ybot,ytop.
  572.  *   This routine uses the cohen & sutherland bit mapping for fast clipping -
  573.  * see "Principles of Interactive Computer Graphics" Newman & Sproull page 65.
  574.  */
  575. static void draw_clip_line(x1, y1, x2, y2)
  576. int x1, y1, x2, y2;
  577. {
  578.     int x, y, dx, dy, x_intr[2], y_intr[2], count, pos1, pos2;
  579.     register struct termentry *t = &term_tbl[term];
  580.  
  581.     pos1 = clip_point_for_clip_line(x1, y1);
  582.     pos2 = clip_point_for_clip_line(x2, y2);
  583.     if (pos1 || pos2) {
  584.     if (pos1 & pos2) return;          /* segment is totally out. */
  585.  
  586.     /* Here part of the segment MAY be inside. test the intersection
  587.      * of this segment with the 4 boundaries for hopefully 2 intersections
  588.      * in. If non found segment is totaly out.
  589.      */
  590.     count = 0;
  591.     dx = x2 - x1;
  592.     dy = y2 - y1;
  593.  
  594.     /* Find intersections with the x parallel bbox lines: */
  595.     if (dy != 0) {
  596.         x = (ybot - y2) * dx / dy + x2;        /* Test for ybot boundary. */
  597.         if (x >= xleft && x <= xright) {
  598.         x_intr[count] = x;
  599.         y_intr[count++] = ybot;
  600.         }
  601.         x = (ytop - y2) * dx / dy + x2;        /* Test for ytop boundary. */
  602.         if (x >= xleft && x <= xright) {
  603.         x_intr[count] = x;
  604.         y_intr[count++] = ytop;
  605.         }
  606.     }
  607.  
  608.     /* Find intersections with the y parallel bbox lines: */
  609.     if (dx != 0) {
  610.         y = (xleft - x2) * dy / dx + y2;      /* Test for xleft boundary. */
  611.         if (y >= ybot && y <= ytop) {
  612.         x_intr[count] = xleft;
  613.         y_intr[count++] = y;
  614.         }
  615.         y = (xright - x2) * dy / dx + y2;    /* Test for xright boundary. */
  616.         if (y >= ybot && y <= ytop) {
  617.         x_intr[count] = xright;
  618.         y_intr[count++] = y;
  619.         }
  620.     }
  621.  
  622.     if (count == 2) {
  623.         int x_max, x_min, y_max, y_min;
  624.  
  625.         x_min = min(x1, x2);
  626.         x_max = max(x1, x2);
  627.         y_min = min(y1, y2);
  628.         y_max = max(y1, y2);
  629.  
  630.         if (pos1 && pos2) {               /* Both were out - update both */
  631.         x1 = x_intr[0];
  632.         y1 = y_intr[0];
  633.         x2 = x_intr[1];
  634.         y2 = y_intr[1];
  635.         }
  636.         else if (pos1) {           /* Only x1/y1 was out - update only it */
  637.         if (dx * (x2 - x_intr[0]) + dy * (y2 - y_intr[0]) > 0) {
  638.             x1 = x_intr[0];
  639.             y1 = y_intr[0];
  640.         }
  641.         else {
  642.             x1 = x_intr[1];
  643.             y1 = y_intr[1];
  644.         }
  645.         }
  646.         else {                      /* Only x2/y2 was out - update only it */
  647.         if (dx * (x_intr[0] - x1) + dy * (y_intr[0] - x1) > 0) {
  648.             x2 = x_intr[0];
  649.             y2 = y_intr[0];
  650.         }
  651.         else {
  652.             x2 = x_intr[1];
  653.             y2 = y_intr[1];
  654.         }
  655.         }
  656.  
  657.         if (x1 < x_min || x1 > x_max ||
  658.         x2 < x_min || x2 > x_max ||
  659.         y1 < y_min || y1 > y_max ||
  660.         y2 < y_min || y2 > y_max) return;
  661.     }
  662.     else
  663.         return;
  664.     }
  665.  
  666.     if (hidden_active) {
  667.     hidden_line_plot(x1, y1, x2, y2);
  668.     }
  669.     else {
  670.     (*t->move)(x1,y1);
  671.     (*t->vector)(x2,y2);
  672.     }
  673. }
  674.  
  675. /* Two routine to emulate move/vector sequence using line drawing routine. */
  676. static int move_pos_x, move_pos_y;
  677.  
  678. static void clip_move(x,y)
  679. int x,y;
  680. {
  681.     move_pos_x = x;
  682.     move_pos_y = y;
  683. }
  684.  
  685. static void clip_vector(x,y)
  686. int x,y;
  687. {
  688.     draw_clip_line(move_pos_x,move_pos_y, x, y);
  689.     move_pos_x = x;
  690.     move_pos_y = y;
  691. }
  692.  
  693. /* And text clipping routine. */
  694. static void clip_put_text(x, y, str)
  695. int x,y;
  696. char *str;
  697. {
  698.     register struct termentry *t = &term_tbl[term];
  699.  
  700.     if (clip_point(x, y)) return;
  701.  
  702.     (*t->put_text)(x,y,str);
  703. }
  704.  
  705. /* (DFK) For some reason, the Sun386i compiler screws up with the CheckLog 
  706.  * macro, so I write it as a function on that machine.
  707.  */
  708. #ifndef sun386
  709. /* (DFK) Use 10^x if logscale is in effect, else x */
  710. #define CheckLog(log, x) ((log) ? pow(10., (x)) : (x))
  711. #else
  712. static double
  713. CheckLog(log, x)
  714.      BOOLEAN log;
  715.      double x;
  716. {
  717.   if (log)
  718.     return(pow(10., x));
  719.   else
  720.     return(x);
  721. }
  722. #endif /* sun386 */
  723.  
  724. static double
  725. LogScale(coord, islog, what, axis)
  726.     double coord;            /* the value */
  727.     BOOLEAN islog;            /* is this axis in logscale? */
  728.     char *what;            /* what is the coord for? */
  729.     char *axis;            /* which axis is this for ("x" or "y")? */
  730. {
  731.     if (islog) {
  732.        if (coord <= 0.0) {
  733.           char errbuf[100];        /* place to write error message */
  734.         (void) sprintf(errbuf,"%s has %s coord of %g; must be above 0 for log scale!",
  735.                 what, axis, coord);
  736.           (*term_tbl[term].text)();
  737.           (void) fflush(outfile);
  738.           int_error(errbuf, NO_CARET);
  739.        } else
  740.         return(log10(coord));
  741.     }
  742.     return(coord);
  743. }
  744.  
  745. /* borders of plotting area */
  746. /* computed once on every call to do_plot */
  747. static boundary3d(scaling)
  748.     BOOLEAN scaling;        /* TRUE if terminal is doing the scaling */
  749. {
  750.     register struct termentry *t = &term_tbl[term];
  751.     xleft = (t->h_char)*2 + (t->h_tic);
  752.     xright = (scaling ? 1 : xsize) * (t->xmax) - (t->h_char)*2 - (t->h_tic);
  753.     ybot = (t->v_char)*5/2 + 1;
  754.     ytop = (scaling ? 1 : ysize) * (t->ymax) - (t->v_char)*5/2 - 1;
  755.     xmiddle = (xright + xleft) / 2;
  756.     ymiddle = (ytop + ybot) / 2;
  757.     xscaler = (xright - xleft) / 2;
  758.     yscaler = (ytop - ybot) / 2;
  759. }
  760.  
  761. static double dbl_raise(x,y)
  762. double x;
  763. int y;
  764. {
  765. register int i;
  766. double val;
  767.  
  768.     val = 1.0;
  769.     for (i=0; i < abs(y); i++)
  770.         val *= x;
  771.     if (y < 0 ) return (1.0/val);
  772.     return(val);
  773. }
  774.  
  775.  
  776. static double make_3dtics(tmin,tmax,axis,logscale)
  777. double tmin,tmax;
  778. int axis;
  779. BOOLEAN logscale;
  780. {
  781. int len,x1,y1,x2,y2;
  782. register double xr,xnorm,tics,tic,l10;
  783.  
  784.     xr = fabs(tmin-tmax);
  785.  
  786.     /* Compute length of axis in screen space coords. */
  787.     switch (axis) {
  788.         case 'x':
  789.             map3d_xy(tmin,0.0,0.0,&x1,&y1);
  790.             map3d_xy(tmax,0.0,0.0,&x2,&y2);
  791.             break;
  792.         case 'y':
  793.             map3d_xy(0.0,tmin,0.0,&x1,&y1);
  794.             map3d_xy(0.0,tmax,0.0,&x2,&y2);
  795.             break;
  796.         case 'z':
  797.             map3d_xy(0.0,0.0,tmin,&x1,&y1);
  798.             map3d_xy(0.0,0.0,tmax,&x2,&y2);
  799.             break;
  800.     }
  801.  
  802.     if (((long) (x1-x2))*(x1-x2) + ((long) (y1-y2))*(y1-y2) <
  803.         sqr(3L * term_tbl[term].h_char))
  804.         return -1.0;                              /* No tics! */
  805.  
  806.     l10 = log10(xr);
  807.     if (logscale) {
  808.         tic = dbl_raise(10.0,(l10 >= 0.0 ) ? (int)l10 : ((int)l10-1));
  809.         if (tic < 1.0)
  810.             tic = 1.0;
  811.     } else {
  812.         xnorm = pow(10.0,l10-(double)((l10 >= 0.0 ) ? (int)l10 : ((int)l10-1)));
  813.         if (xnorm <= 5)
  814.             tics = 0.5;
  815.         else tics = 1.0;
  816.         tic = tics * dbl_raise(10.0,(l10 >= 0.0 ) ? (int)l10 : ((int)l10-1));
  817.     }
  818.     return(tic);
  819. }
  820.  
  821. do_3dplot(plots, pcount, min_x, max_x, min_y, max_y, min_z, max_z)
  822. struct surface_points *plots;
  823. int pcount;            /* count of plots in linked list */
  824. double min_x, max_x;
  825. double min_y, max_y;
  826. double min_z, max_z;
  827. {
  828. register struct termentry *t = &term_tbl[term];
  829. register int surface, xaxis_y, yaxis_x;
  830. register struct surface_points *this_plot;
  831. int xl, yl;
  832.             /* only a Pyramid would have this many registers! */
  833. double xtemp, ytemp, ztemp, temp;
  834. struct text_label *this_label;
  835. struct arrow_def *this_arrow;
  836. BOOLEAN scaling;
  837. transform_matrix mat;
  838.  
  839. /* Initiate transformation matrix using the global view variables. */
  840.     mat_rot_z(surface_rot_z, trans_mat);
  841.     mat_rot_x(surface_rot_x, mat);
  842.     mat_mult(trans_mat, trans_mat, mat);
  843.     mat_scale(surface_scale / 2.0, surface_scale / 2.0, surface_scale / 2.0, mat);
  844.     mat_mult(trans_mat, trans_mat, mat);
  845.  
  846. /* modify min_z/max_z so it will zscale properly. */
  847.     ztemp = (max_z - min_z) / (2.0 * surface_zscale);
  848.     temp = (max_z + min_z) / 2.0;
  849.     min_z = temp - ztemp;
  850.     max_z = temp + ztemp;
  851.  
  852. /* store these in variables global to this file */
  853. /* otherwise, we have to pass them around a lot */
  854.     x_min3d = min_x;
  855.     x_max3d = max_x;
  856.     y_min3d = min_y;
  857.     y_max3d = max_y;
  858.     z_min3d = min_z;
  859.     z_max3d = max_z;
  860.  
  861.     if (polar)
  862.     int_error("Can not splot in polar coordinate system.", NO_CARET);
  863.  
  864.     if (z_min3d == VERYLARGE || z_max3d == -VERYLARGE ||
  865.     x_min3d == VERYLARGE || x_max3d == -VERYLARGE ||
  866.     y_min3d == VERYLARGE || y_max3d == -VERYLARGE)
  867.         int_error("all points undefined!", NO_CARET);
  868.  
  869.     /* If we are to draw the bottom grid make sure zmin is updated properly. */
  870.     if (xtics || ytics || grid)
  871.     z_min3d -= (max_z - min_z) * ticslevel;
  872.  
  873. /*  This used be x_max3d == x_min3d, but that caused an infinite loop once. */
  874.     if (fabs(x_max3d - x_min3d) < zero)
  875.     int_error("x_min3d should not equal x_max3d!",NO_CARET);
  876.     if (fabs(y_max3d - y_min3d) < zero)
  877.     int_error("y_min3d should not equal y_max3d!",NO_CARET);
  878.     if (fabs(z_max3d - z_min3d) < zero)
  879.     int_error("z_min3d should not equal z_max3d!",NO_CARET);
  880.  
  881.     if (hidden3d) {
  882.     struct surface_points *plot;
  883.     
  884.         /* Verify data is hidden line removable - nonparametric + grid based. */
  885.         for (plot = plots; plot != NULL; plot = plot->next_sp) {
  886.         if (parametric ||
  887.             (plot->plot_type == DATA3D && !plot->has_grid_topology))
  888.             int_error("Cannot hidden line remove parametric surface or non grid data\n", NO_CARET);
  889.         }
  890.     }
  891.  
  892. /* INITIALIZE TERMINAL */
  893.     if (!term_init) {
  894.     (*t->init)();
  895.     term_init = TRUE;
  896.     }
  897.     screen_ok = FALSE;
  898.     scaling = (*t->scale)(xsize, ysize);
  899.     (*t->graphics)();
  900.  
  901.     /* now compute boundary for plot (xleft, xright, ytop, ybot) */
  902.     boundary3d(scaling);
  903.  
  904. /* SCALE FACTORS */
  905.     zscale3d = 2.0/(z_max3d - z_min3d);
  906.     yscale3d = 2.0/(y_max3d - y_min3d);
  907.     xscale3d = 2.0/(x_max3d - x_min3d);
  908.  
  909.     (*t->linetype)(-2); /* border linetype */
  910.  
  911. /* PLACE TITLE */
  912.     if (*title != 0) {
  913.         int x, y;
  914.  
  915.         x = title_xoffset * t->h_char;
  916.         y = title_yoffset * t->v_char;
  917.  
  918.         if ((*t->justify_text)(CENTRE)) 
  919.             (*t->put_text)(x+(xleft+xright)/2, 
  920.                        y+ytop+(t->v_char), title);
  921.         else
  922.             (*t->put_text)(x+(xleft+xright)/2 - strlen(title)*(t->h_char)/2,
  923.                        y+ytop+(t->v_char), title);
  924.     }
  925.  
  926. /* PLACE TIMEDATE */
  927.     if (timedate) {
  928.         int x, y;
  929.  
  930.         x = time_xoffset * t->h_char;
  931.         y = time_yoffset * t->v_char;
  932.         dated = time( (long *) 0);
  933.         tdate = ctime( &dated);
  934.         tdate[24]='\0';
  935.         if ((*t->text_angle)(1)) {
  936.             if ((*t->justify_text)(CENTRE)) {
  937.                 (*t->put_text)(x+(t->v_char),
  938.                          y+ybot+4*(t->v_char), tdate);
  939.             }
  940.             else {
  941.                 (*t->put_text)(x+(t->v_char),
  942.                          y+ybot+4*(t->v_char)-(t->h_char)*strlen(ylabel)/2, 
  943.                          tdate);
  944.             }
  945.         }
  946.         else {
  947.             (void)(*t->justify_text)(LEFT);
  948.             (*t->put_text)(x,
  949.                          y+ybot-3*(t->v_char), tdate);
  950.         }
  951.         (void)(*t->text_angle)(0);
  952.     }
  953.  
  954. /* PLACE LABELS */
  955.     for (this_label = first_label; this_label!=NULL;
  956.             this_label=this_label->next ) {
  957.         int x,y;
  958.  
  959.         xtemp = LogScale(this_label->x, log_x, "label", "x");
  960.         ytemp = LogScale(this_label->y, log_y, "label", "y");
  961.         ztemp = LogScale(this_label->z, log_z, "label", "z");
  962.             map3d_xy(xtemp,ytemp,ztemp, &x, &y);
  963.  
  964.         if ((*t->justify_text)(this_label->pos)) {
  965.             (*t->put_text)(x,y,this_label->text);
  966.         }
  967.         else {
  968.             switch(this_label->pos) {
  969.                 case  LEFT:
  970.                     (*t->put_text)(x,y,this_label->text);
  971.                     break;
  972.                 case CENTRE:
  973.                     (*t->put_text)(x -
  974.                         (t->h_char)*strlen(this_label->text)/2,
  975.                         y, this_label->text);
  976.                     break;
  977.                 case RIGHT:
  978.                     (*t->put_text)(x -
  979.                         (t->h_char)*strlen(this_label->text),
  980.                         y, this_label->text);
  981.                     break;
  982.             }
  983.          }
  984.      }
  985.  
  986. /* PLACE ARROWS */
  987.     (*t->linetype)(0);    /* arrow line type */
  988.     for (this_arrow = first_arrow; this_arrow!=NULL;
  989.         this_arrow = this_arrow->next ) {
  990.     int sx,sy,ex,ey;
  991.  
  992.     xtemp = LogScale(this_arrow->sx, log_x, "arrow", "x");
  993.     ytemp = LogScale(this_arrow->sy, log_y, "arrow", "y");
  994.     ztemp = LogScale(this_arrow->sz, log_y, "arrow", "z");
  995.     map3d_xy(xtemp,ytemp,ztemp, &sx, &sy);
  996.  
  997.     xtemp = LogScale(this_arrow->ex, log_x, "arrow", "x");
  998.     ytemp = LogScale(this_arrow->ey, log_y, "arrow", "y");
  999.     ztemp = LogScale(this_arrow->ez, log_y, "arrow", "z");
  1000.     map3d_xy(xtemp,ytemp,ztemp, &ex, &ey);
  1001.  
  1002.     (*t->arrow)(sx, sy, ex, ey, this_arrow->head);
  1003.     }
  1004.  
  1005.     if (hidden3d) {
  1006.     init_hidden_line_removal();
  1007.     reset_hidden_line_removal();
  1008.     hidden_active = TRUE;
  1009.     }
  1010.  
  1011. /* DRAW SURFACES AND CONTOURS */
  1012.     real_z_min3d = min_z;
  1013.     real_z_max3d = max_z;
  1014.     if (key == -1) {
  1015.         xl = xright  - (t->h_tic) - (t->h_char)*5;
  1016.         yl = ytop - (t->v_tic) - (t->v_char);
  1017.     }
  1018.     if (key == 1) {
  1019.         xtemp = LogScale(key_x, log_x, "key", "x");
  1020.         ytemp = LogScale(key_y, log_y, "key", "y");
  1021.         ztemp = LogScale(key_z, log_z, "key", "z");
  1022.         map3d_xy(xtemp,ytemp,ztemp, &xl, &yl);
  1023.     }
  1024.  
  1025.     this_plot = plots;
  1026.     for (surface = 0;
  1027.          surface < pcount;
  1028.          this_plot = this_plot->next_sp, surface++) {
  1029.         if ( hidden3d )
  1030.             hidden_no_update = FALSE;
  1031.  
  1032.         if (draw_surface) {
  1033.             (*t->linetype)(this_plot->line_type);
  1034.             if (hidden3d) {
  1035.             hidden_line_type_above = this_plot->line_type;
  1036.             hidden_line_type_below = this_plot->line_type + 1;
  1037.             }
  1038.             
  1039.             if (key != 0) {
  1040.             if ((*t->justify_text)(RIGHT)) {
  1041.                 clip_put_text(xl,
  1042.                       yl,this_plot->title);
  1043.             }
  1044.             else {
  1045.                 if (inrange(xl-(t->h_char)*strlen(this_plot->title), 
  1046.                     xleft, xright))
  1047.                 clip_put_text(xl-(t->h_char)*strlen(this_plot->title),
  1048.                           yl,this_plot->title);
  1049.             }
  1050.             }
  1051.             
  1052.             switch(this_plot->plot_style) {
  1053.                 case IMPULSES: {
  1054.                 if (key != 0) {
  1055.                 clip_move(xl+(t->h_char),yl);
  1056.                 clip_vector(xl+4*(t->h_char),yl);
  1057.                 }
  1058.                 plot3d_impulses(this_plot);
  1059.                 break;
  1060.             }
  1061.             case LINES: {
  1062.                 if (key != 0) {
  1063.                 clip_move(xl+(int)(t->h_char),yl);
  1064.                 clip_vector(xl+(int)(4*(t->h_char)),yl);
  1065.                 }
  1066.                 plot3d_lines(this_plot);
  1067.                 break;
  1068.             }
  1069.             case ERRORBARS:    /* ignored; treat like points */
  1070.             case POINTS: {
  1071.                 if (key != 0 && !clip_point(xl+2*(t->h_char),yl)) {
  1072.                 (*t->point)(xl+2*(t->h_char),yl,
  1073.                         this_plot->point_type);
  1074.                 }
  1075.                 plot3d_points(this_plot);
  1076.                 break;
  1077.             }
  1078.             case LINESPOINTS: {
  1079.                 /* put lines */
  1080.                 if (key != 0) {
  1081.                 clip_move(xl+(t->h_char),yl);
  1082.                 clip_vector(xl+4*(t->h_char),yl);
  1083.                 }
  1084.                 plot3d_lines(this_plot);
  1085.             
  1086.                 /* put points */
  1087.                 if (key != 0 && !clip_point(xl+2*(t->h_char),yl)) {
  1088.                 (*t->point)(xl+2*(t->h_char),yl,
  1089.                         this_plot->point_type);
  1090.                 }
  1091.                 plot3d_points(this_plot);
  1092.                 break;
  1093.             }
  1094.             case DOTS: {
  1095.                 if (key != 0 && !clip_point(xl+2*(t->h_char),yl)) {
  1096.                 (*t->point)(xl+2*(t->h_char),yl, -1);
  1097.                 }
  1098.                 plot3d_dots(this_plot);
  1099.                 break;
  1100.             }
  1101.             }
  1102.             yl = yl - (t->v_char);
  1103.         }
  1104.  
  1105.         if ( hidden3d ) {
  1106.             hidden_no_update = TRUE;
  1107.             hidden_line_type_above = this_plot->line_type + (hidden3d ? 2 : 1);
  1108.             hidden_line_type_below = this_plot->line_type + (hidden3d ? 2 : 1);
  1109.         }
  1110.  
  1111.         if (draw_contour && this_plot->contours != NULL) {
  1112.             struct gnuplot_contours *cntrs = this_plot->contours;
  1113.  
  1114.             (*t->linetype)(this_plot->line_type + (hidden3d ? 2 : 1));
  1115.  
  1116.             if (key != 0) {
  1117.                 if ((*t->justify_text)(RIGHT)) {
  1118.                     clip_put_text(xl,
  1119.                         yl,this_plot->title);
  1120.                 }
  1121.                 else {
  1122.                     if (inrange(xl-(t->h_char)*strlen(this_plot->title), 
  1123.                              xleft, xright))
  1124.                      clip_put_text(xl-(t->h_char)*strlen(this_plot->title),
  1125.                                  yl,this_plot->title);
  1126.                 }
  1127.                 switch(this_plot->plot_style) {
  1128.                     case IMPULSES:
  1129.                         clip_move(xl+(t->h_char),yl);
  1130.                         clip_vector(xl+4*(t->h_char),yl);
  1131.                         break;
  1132.                     case LINES:
  1133.                         clip_move(xl+(int)(t->h_char),yl);
  1134.                         clip_vector(xl+(int)(4*(t->h_char)),yl);
  1135.                         break;
  1136.                     case ERRORBARS: /* ignored; treat like points */
  1137.                     case POINTS:
  1138.                         if (!clip_point(xl+2*(t->h_char),yl)) {
  1139.                              (*t->point)(xl+2*(t->h_char),yl,
  1140.                                     this_plot->point_type);
  1141.                         }
  1142.                         break;
  1143.                     case LINESPOINTS:
  1144.                         clip_move(xl+(int)(t->h_char),yl);
  1145.                         clip_vector(xl+(int)(4*(t->h_char)),yl);
  1146.                         break;
  1147.                     case DOTS:
  1148.                         if (!clip_point(xl+2*(t->h_char),yl)) {
  1149.                              (*t->point)(xl+2*(t->h_char),yl, -1);
  1150.                         }
  1151.                         break;
  1152.                 }
  1153.             }
  1154.  
  1155.             while (cntrs) {
  1156.                 switch(this_plot->plot_style) {
  1157.                     case IMPULSES:
  1158.                            cntr3d_impulses(cntrs, this_plot);
  1159.                         break;
  1160.                     case LINES:
  1161.                         cntr3d_lines(cntrs);
  1162.                         break;
  1163.                     case ERRORBARS: /* ignored; treat like points */
  1164.                     case POINTS:
  1165.                         cntr3d_points(cntrs, this_plot);
  1166.                         break;
  1167.                     case LINESPOINTS:
  1168.                         cntr3d_lines(cntrs);
  1169.                         cntr3d_points(cntrs, this_plot);
  1170.                         break;
  1171.                     case DOTS:
  1172.                         cntr3d_dots(cntrs);
  1173.                         break;
  1174.                 }
  1175.                 cntrs = cntrs->next;
  1176.             }
  1177.             if (key != 0) yl = yl - (t->v_char);
  1178.         }
  1179.  
  1180.         if (surface == 0)
  1181.             draw_bottom_grid(this_plot,real_z_min3d,real_z_max3d);
  1182.     }
  1183.     (*t->text)();
  1184.     (void) fflush(outfile);
  1185.  
  1186.     if (hidden3d) {
  1187.         term_hidden_line_removal();
  1188.         hidden_active = FALSE;
  1189.     }
  1190. }
  1191.  
  1192. /* plot3d_impulses:
  1193.  * Plot the surfaces in IMPULSES style
  1194.  */
  1195. static plot3d_impulses(plot)
  1196.     struct surface_points *plot;
  1197. {
  1198.     int i;                /* point index */
  1199.     int x,y,x0,y0;            /* point in terminal coordinates */
  1200.     struct termentry *t = &term_tbl[term];
  1201.     struct iso_curve *icrvs = plot->iso_crvs;
  1202.  
  1203.     while ( icrvs ) {
  1204.     struct coordinate *points = icrvs->points;
  1205.  
  1206.     for (i = 0; i < icrvs->p_count; i++) {
  1207.         if (real_z_max3d<points[i].z)
  1208.         real_z_max3d=points[i].z;
  1209.         if (real_z_min3d>points[i].z)
  1210.         real_z_min3d=points[i].z;
  1211.  
  1212.         map3d_xy(points[i].x, points[i].y, points[i].z, &x, &y);
  1213.         map3d_xy(points[i].x, points[i].y, z_min3d, &x0, &y0);
  1214.  
  1215.         clip_move(x0,y0);
  1216.         clip_vector(x,y);
  1217.     }
  1218.  
  1219.     icrvs = icrvs->next;
  1220.     }
  1221. }
  1222.  
  1223. /* plot3d_lines:
  1224.  * Plot the surfaces in LINES style
  1225.  */
  1226. static plot3d_lines(plot)
  1227.     struct surface_points *plot;
  1228. {
  1229.     int i, iso_count, num_iso_lines;
  1230.     int x,y;                /* point in terminal coordinates */
  1231.     struct termentry *t = &term_tbl[term];
  1232.     struct iso_curve *icrvs = plot->iso_crvs,
  1233.        *first_row_icrv, *last_row_icrv, *first_col_icrv, *icrv;
  1234.     struct coordinate *points;
  1235.  
  1236.     if (hidden3d) {
  1237.     hidden_no_update = FALSE;
  1238.  
  1239.     if (plot->plot_type == FUNC3D)
  1240.         num_iso_lines = iso_samples;
  1241.     else
  1242.         num_iso_lines = plot->num_iso_read;
  1243.  
  1244.     icrvs = reorder_hidden_iso_curves(plot, num_iso_lines);
  1245.  
  1246.     first_row_icrv = icrvs;
  1247.     for (last_row_icrv = icrvs, i = 1;
  1248.          i++ < num_iso_lines;
  1249.          last_row_icrv = last_row_icrv->next);
  1250.     first_col_icrv = last_row_icrv->next;
  1251.     reset_hidden_line_removal();
  1252.  
  1253.     iso_count = 1;
  1254.     }
  1255.  
  1256.     while (icrvs) {
  1257.     if (hidden3d) {
  1258.         if (iso_count == 1 || iso_count == num_iso_lines + 1) {
  1259.         hidden_first_row = TRUE;
  1260.  
  1261.         /* Draw other boundary so low/high bounds will be complete: */
  1262.         icrv = iso_count == 1 ? first_col_icrv : first_row_icrv;
  1263.         for (i = 0, points = icrv->points; i < icrv->p_count; i++) {
  1264.             map3d_xy(points[i].x, points[i].y, points[i].z, &x, &y);
  1265.  
  1266.             if (i > 0)
  1267.             clip_vector(x,y);
  1268.             else
  1269.             clip_move(x,y);
  1270.         }
  1271.         }
  1272.         else
  1273.         hidden_first_row = FALSE;
  1274.         iso_count++;
  1275.     }
  1276.  
  1277.     for (i = 0, points = icrvs->points; i < icrvs->p_count; i++) {
  1278.         if (real_z_max3d<points[i].z)
  1279.         real_z_max3d=points[i].z;
  1280.         if (real_z_min3d>points[i].z)
  1281.         real_z_min3d=points[i].z;
  1282.  
  1283.         map3d_xy(points[i].x, points[i].y, points[i].z, &x, &y);
  1284.  
  1285.         if (i > 0)
  1286.         clip_vector(x,y);
  1287.         else
  1288.         clip_move(x,y);
  1289.     }
  1290.  
  1291.     icrvs = icrvs->next;
  1292.     }
  1293.  
  1294.     if (hidden3d) {
  1295.     /* Draw other boundary so low/high bounds will be complete: */
  1296.     for (i = 0, points = last_row_icrv->points;
  1297.          i < last_row_icrv->p_count; i++) {
  1298.         map3d_xy(points[i].x, points[i].y, points[i].z, &x, &y);
  1299.  
  1300.         if (i > 0)
  1301.         clip_vector(x,y);
  1302.         else
  1303.         clip_move(x,y);
  1304.     }
  1305.     }
  1306. }
  1307.  
  1308. /* plot3d_points:
  1309.  * Plot the surfaces in POINTS style
  1310.  */
  1311. static plot3d_points(plot)
  1312.     struct surface_points *plot;
  1313. {
  1314.     int i,x,y;
  1315.     struct termentry *t = &term_tbl[term];
  1316.     struct iso_curve *icrvs = plot->iso_crvs;
  1317.  
  1318.     while ( icrvs ) {
  1319.     struct coordinate *points = icrvs->points;
  1320.  
  1321.     for (i = 0; i < icrvs->p_count; i++) {
  1322.         if (real_z_max3d<points[i].z)
  1323.         real_z_max3d=points[i].z;
  1324.         if (real_z_min3d>points[i].z)
  1325.         real_z_min3d=points[i].z;
  1326.  
  1327.         map3d_xy(points[i].x, points[i].y, points[i].z, &x, &y);
  1328.  
  1329.         if (!clip_point(x,y))
  1330.         (*t->point)(x,y, plot->point_type);
  1331.     }
  1332.  
  1333.     icrvs = icrvs->next;
  1334.     }
  1335. }
  1336.  
  1337. /* plot3d_dots:
  1338.  * Plot the surfaces in DOTS style
  1339.  */
  1340. static plot3d_dots(plot)
  1341.     struct surface_points *plot;
  1342. {
  1343.     int i,x,y;
  1344.     struct termentry *t = &term_tbl[term];
  1345.     struct iso_curve *icrvs = plot->iso_crvs;
  1346.  
  1347.     while ( icrvs ) {
  1348.     struct coordinate *points = icrvs->points;
  1349.  
  1350.         for (i = 0; i < icrvs->p_count; i++) {
  1351.         if (real_z_max3d<points[i].z)
  1352.         real_z_max3d=points[i].z;
  1353.         if (real_z_min3d>points[i].z)
  1354.             real_z_min3d=points[i].z;
  1355.  
  1356.             map3d_xy(points[i].x, points[i].y, points[i].z, &x, &y);
  1357.  
  1358.             if (!clip_point(x,y))
  1359.         (*t->point)(x,y, -1);
  1360.         }
  1361.  
  1362.     icrvs = icrvs->next;
  1363.     }
  1364. }
  1365.  
  1366. /* cntr3d_impulses:
  1367.  * Plot a surface contour in IMPULSES style
  1368.  */
  1369. static cntr3d_impulses(cntr, plot)
  1370.     struct gnuplot_contours *cntr;
  1371.     struct surface_points *plot;
  1372. {
  1373.     int i,j,k;                /* point index */
  1374.     int x,y,x0,y0;            /* point in terminal coordinates */
  1375.     struct termentry *t = &term_tbl[term];
  1376.  
  1377.     if (draw_contour == CONTOUR_SRF || draw_contour == CONTOUR_BOTH) {
  1378.     for (i = 0; i < cntr->num_pts; i++) {
  1379.         if (real_z_max3d<cntr->coords[i].z)
  1380.         real_z_max3d=cntr->coords[i].z;
  1381.         if (real_z_min3d>cntr->coords[i].z)
  1382.         real_z_min3d=cntr->coords[i].z;
  1383.  
  1384.         map3d_xy(cntr->coords[i].x, cntr->coords[i].y, cntr->coords[i].z,
  1385.              &x, &y);
  1386.         map3d_xy(cntr->coords[i].x, cntr->coords[i].y, z_min3d,
  1387.              &x0, &y0);
  1388.  
  1389.         clip_move(x0,y0);
  1390.         clip_vector(x,y);
  1391.     }
  1392.     }
  1393.     else
  1394.     cntr3d_points(cntr, plot);   /* Must be on base grid, so do points. */
  1395. }
  1396.  
  1397. /* cntr3d_lines:
  1398.  * Plot a surface contour in LINES style
  1399.  */
  1400. static cntr3d_lines(cntr)
  1401.     struct gnuplot_contours *cntr;
  1402. {
  1403.     int i,j,k;                /* point index */
  1404.     int x,y;                /* point in terminal coordinates */
  1405.     struct termentry *t = &term_tbl[term];
  1406.  
  1407.     if (draw_contour == CONTOUR_SRF || draw_contour == CONTOUR_BOTH) {
  1408.     for (i = 0; i < cntr->num_pts; i++) {
  1409.         if (real_z_max3d<cntr->coords[i].z)
  1410.         real_z_max3d=cntr->coords[i].z;
  1411.         if (real_z_min3d>cntr->coords[i].z)
  1412.         real_z_min3d=cntr->coords[i].z;
  1413.  
  1414.             map3d_xy(cntr->coords[i].x, cntr->coords[i].y, cntr->coords[i].z,
  1415.                  &x, &y);
  1416.  
  1417.             if (i > 0)
  1418.             clip_vector(x,y);
  1419.         else
  1420.         clip_move(x,y);
  1421.         }
  1422.     }
  1423.  
  1424.     if (draw_contour == CONTOUR_BASE || draw_contour == CONTOUR_BOTH) {
  1425.     for (i = 0; i < cntr->num_pts; i++) {
  1426.         if (real_z_max3d<cntr->coords[i].z)
  1427.         real_z_max3d=cntr->coords[i].z;
  1428.         if (real_z_min3d>cntr->coords[i].z)
  1429.         real_z_min3d=cntr->coords[i].z;
  1430.  
  1431.             map3d_xy(cntr->coords[i].x, cntr->coords[i].y, z_min3d,
  1432.                  &x, &y);
  1433.  
  1434.             if (i > 0)
  1435.             clip_vector(x,y);
  1436.         else
  1437.         clip_move(x,y);
  1438.         }
  1439.     }
  1440. }
  1441.  
  1442. /* cntr3d_points:
  1443.  * Plot a surface contour in POINTS style
  1444.  */
  1445. static cntr3d_points(cntr, plot)
  1446.     struct gnuplot_contours *cntr;
  1447.     struct surface_points *plot;
  1448. {
  1449.     int i,j,k;
  1450.     int x,y;
  1451.     struct termentry *t = &term_tbl[term];
  1452.  
  1453.     if (draw_contour == CONTOUR_SRF || draw_contour == CONTOUR_BOTH) {
  1454.     for (i = 0; i < cntr->num_pts; i++) {
  1455.         if (real_z_max3d<cntr->coords[i].z)
  1456.         real_z_max3d=cntr->coords[i].z;
  1457.         if (real_z_min3d>cntr->coords[i].z)
  1458.         real_z_min3d=cntr->coords[i].z;
  1459.  
  1460.             map3d_xy(cntr->coords[i].x, cntr->coords[i].y, cntr->coords[i].z,
  1461.                  &x, &y);
  1462.  
  1463.         if (!clip_point(x,y))
  1464.         (*t->point)(x,y, plot->point_type);
  1465.         }
  1466.     }
  1467.  
  1468.     if (draw_contour == CONTOUR_BASE || draw_contour == CONTOUR_BOTH) {
  1469.     for (i = 0; i < cntr->num_pts; i++) {
  1470.         if (real_z_max3d<cntr->coords[i].z)
  1471.         real_z_max3d=cntr->coords[i].z;
  1472.         if (real_z_min3d>cntr->coords[i].z)
  1473.         real_z_min3d=cntr->coords[i].z;
  1474.  
  1475.             map3d_xy(cntr->coords[i].x, cntr->coords[i].y, z_min3d,
  1476.                  &x, &y);
  1477.  
  1478.         if (!clip_point(x,y))
  1479.         (*t->point)(x,y, plot->point_type);
  1480.         }
  1481.     }
  1482. }
  1483.  
  1484. /* cntr3d_dots:
  1485.  * Plot a surface contour in DOTS style
  1486.  */
  1487. static cntr3d_dots(cntr)
  1488.     struct gnuplot_contours *cntr;
  1489. {
  1490.     int i,j,k;
  1491.     int x,y;
  1492.     struct termentry *t = &term_tbl[term];
  1493.  
  1494.     if (draw_contour == CONTOUR_SRF || draw_contour == CONTOUR_BOTH) {
  1495.     for (i = 0; i < cntr->num_pts; i++) {
  1496.         if (real_z_max3d<cntr->coords[i].z)
  1497.         real_z_max3d=cntr->coords[i].z;
  1498.         if (real_z_min3d>cntr->coords[i].z)
  1499.         real_z_min3d=cntr->coords[i].z;
  1500.  
  1501.             map3d_xy(cntr->coords[i].x, cntr->coords[i].y, cntr->coords[i].z,
  1502.                  &x, &y);
  1503.  
  1504.         if (!clip_point(x,y))
  1505.         (*t->point)(x,y, -1);
  1506.         }
  1507.     }
  1508.  
  1509.     if (draw_contour == CONTOUR_BASE || draw_contour == CONTOUR_BOTH) {
  1510.     for (i = 0; i < cntr->num_pts; i++) {
  1511.         if (real_z_max3d<cntr->coords[i].z)
  1512.         real_z_max3d=cntr->coords[i].z;
  1513.         if (real_z_min3d>cntr->coords[i].z)
  1514.         real_z_min3d=cntr->coords[i].z;
  1515.  
  1516.             map3d_xy(cntr->coords[i].x, cntr->coords[i].y, z_min3d,
  1517.                  &x, &y);
  1518.  
  1519.         if (!clip_point(x,y))
  1520.         (*t->point)(x,y, -1);
  1521.         }
  1522.     }
  1523. }
  1524.  
  1525. static update_extrema_pts(ix, iy, min_sx_x, min_sx_y, min_sy_x, min_sy_y,
  1526.               x, y)
  1527.     int ix, iy, *min_sx_x, *min_sx_y, *min_sy_x, *min_sy_y;
  1528.     double x, y;
  1529. {
  1530.  
  1531.     if (*min_sx_x > ix + 2 ||         /* find (bottom) left corner of grid */
  1532.     (abs(*min_sx_x - ix) <= 2 && *min_sx_y > iy)) {
  1533.     *min_sx_x = ix;
  1534.     *min_sx_y = iy;
  1535.     min_sx_ox = x;
  1536.     min_sx_oy = y;
  1537.     }
  1538.     if (*min_sy_y > iy + 2 ||         /* find bottom (right) corner of grid */
  1539.     (abs(*min_sy_y - iy) <= 2 && *min_sy_x < ix)) {
  1540.     *min_sy_x = ix;
  1541.     *min_sy_y = iy;
  1542.     min_sy_ox = x;
  1543.     min_sy_oy = y;
  1544.     }
  1545. }
  1546.  
  1547. /* Draw the bottom grid for the parametric case. */
  1548. static draw_parametric_grid(plot)
  1549.     struct surface_points *plot;
  1550. {
  1551.     int i,ix,iy,            /* point in terminal coordinates */
  1552.     min_sx_x = 10000,min_sx_y = 10000,min_sy_x = 10000,min_sy_y = 10000,
  1553.         grid_iso = plot->plot_type == DATA3D && plot->has_grid_topology ?
  1554.                     plot->num_iso_read : iso_samples;
  1555.     double x,y,dx,dy;
  1556.     struct termentry *t = &term_tbl[term];
  1557.  
  1558.     if (grid && plot->has_grid_topology) {
  1559.         x = x_min3d;
  1560.         y = y_min3d;
  1561.  
  1562.     dx = (x_max3d-x_min3d) / (grid_iso-1);
  1563.     dy = (y_max3d-y_min3d) / (grid_iso-1);
  1564.  
  1565.     for (i = 0; i < grid_iso; i++) {
  1566.             if (i == 0 || i == grid_iso-1)            
  1567.             setlinestyle(-2);
  1568.         else
  1569.             setlinestyle(-1);
  1570.         map3d_xy(x_min3d, y, z_min3d, &ix, &iy);
  1571.         clip_move(ix,iy);
  1572.         update_extrema_pts(ix,iy,&min_sx_x,&min_sx_y,
  1573.                    &min_sy_x,&min_sy_y,x_min3d,y);
  1574.  
  1575.         map3d_xy(x_max3d, y, z_min3d, &ix, &iy);
  1576.         clip_vector(ix,iy);
  1577.         update_extrema_pts(ix,iy,&min_sx_x,&min_sx_y,
  1578.                    &min_sy_x,&min_sy_y,x_max3d,y);
  1579.  
  1580.         y += dy;
  1581.     }
  1582.  
  1583.     for (i = 0; i < grid_iso; i++) {
  1584.             if (i == 0 || i == grid_iso-1)
  1585.             setlinestyle(-2);
  1586.         else
  1587.             setlinestyle(-1);
  1588.         map3d_xy(x, y_min3d, z_min3d, &ix, &iy);
  1589.         clip_move(ix,iy);
  1590.         update_extrema_pts(ix,iy,&min_sx_x,&min_sx_y,
  1591.                    &min_sy_x,&min_sy_y,x,y_min3d);
  1592.  
  1593.         map3d_xy(x, y_max3d, z_min3d, &ix, &iy);
  1594.         clip_vector(ix,iy);
  1595.         update_extrema_pts(ix,iy,&min_sx_x,&min_sx_y,
  1596.                    &min_sy_x,&min_sy_y,x,y_max3d);
  1597.  
  1598.         x += dx;
  1599.     }
  1600.     }
  1601.     else {
  1602.     setlinestyle(-2);
  1603.  
  1604.     map3d_xy(x_min3d, y_min3d, z_min3d, &ix, &iy);
  1605.     clip_move(ix,iy);
  1606.     update_extrema_pts(ix,iy,&min_sx_x,&min_sx_y,
  1607.                &min_sy_x,&min_sy_y,x_min3d,y_min3d);
  1608.  
  1609.     map3d_xy(x_max3d, y_min3d, z_min3d, &ix, &iy);
  1610.     clip_vector(ix,iy);
  1611.     update_extrema_pts(ix,iy,&min_sx_x,&min_sx_y,
  1612.                &min_sy_x,&min_sy_y,x_max3d,y_min3d);
  1613.  
  1614.     map3d_xy(x_max3d, y_max3d, z_min3d, &ix, &iy);
  1615.     clip_vector(ix,iy);
  1616.     update_extrema_pts(ix,iy,&min_sx_x,&min_sx_y,
  1617.                &min_sy_x,&min_sy_y,x_max3d,y_max3d);
  1618.  
  1619.     map3d_xy(x_min3d, y_max3d, z_min3d, &ix, &iy);
  1620.     clip_vector(ix,iy);
  1621.     update_extrema_pts(ix,iy,&min_sx_x,&min_sx_y,
  1622.                &min_sy_x,&min_sy_y,x_min3d,y_max3d);
  1623.  
  1624.  
  1625.     map3d_xy(x_min3d, y_min3d, z_min3d, &ix, &iy);
  1626.     clip_vector(ix,iy);
  1627.     }
  1628. }
  1629.  
  1630. /* Draw the bottom grid for non parametric case. */
  1631. static draw_non_param_grid(plot)
  1632.     struct surface_points *plot;
  1633. {
  1634.     int i,is_boundary=TRUE,crv_count=0,
  1635.     x,y,                /* point in terminal coordinates */
  1636.     min_sx_x = 10000,min_sx_y = 10000,min_sy_x = 10000,min_sy_y = 10000,
  1637.         grid_samples = plot->plot_type == DATA3D && plot->has_grid_topology ?
  1638.                     plot->iso_crvs->p_count : samples,
  1639.         grid_iso = plot->plot_type == DATA3D && plot->has_grid_topology ?
  1640.                     plot->num_iso_read : iso_samples;
  1641.     struct termentry *t = &term_tbl[term];
  1642.     struct iso_curve *icrvs = plot->iso_crvs;
  1643.  
  1644.     while ( icrvs ) {
  1645.     struct coordinate *points = icrvs->points;
  1646.     int saved_hidden_active = hidden_active;
  1647.     double z1 = map3d_z(points[0].x, points[0].y, 0.0),
  1648.            z2 = map3d_z(points[icrvs->p_count-1].x,
  1649.                             points[icrvs->p_count-1].y, 0.0);
  1650.  
  1651.     for (i = 0; i < icrvs->p_count; i += icrvs->p_count-1) {
  1652.         map3d_xy(points[i].x, points[i].y, z_min3d, &x, &y);
  1653.         if (is_boundary) {
  1654.         setlinestyle(-2);
  1655.         }
  1656.         else {
  1657.             setlinestyle(-1);
  1658.         }
  1659.  
  1660.         if (i > 0) {
  1661.             clip_vector(x,y);
  1662.         }
  1663.         else {
  1664.             clip_move(x,y);
  1665.         }
  1666.  
  1667.         if (draw_surface &&
  1668.             is_boundary &&
  1669.             (i == 0 || i == icrvs->p_count-1)) {
  1670.             int x1,y1;            /* point in terminal coordinates */
  1671.  
  1672.         /* Draw a vertical line to surface corner from grid corner. */
  1673.             map3d_xy(points[i].x, points[i].y, points[i].z, &x1, &y1);
  1674.             if (hidden3d) {
  1675.             if ((i == 0 && z1 > z2) ||
  1676.                 (i == icrvs->p_count-1 && z2 > z1)) {
  1677.                 hidden_active = FALSE; /* This one is always visible. */
  1678.             }                
  1679.             }
  1680.             clip_vector(x1,y1);
  1681.             clip_move(x,y);
  1682.         hidden_active = saved_hidden_active;
  1683.         update_extrema_pts(x,y,&min_sx_x,&min_sx_y, &min_sy_x,&min_sy_y,
  1684.                    points[i].x,points[i].y);
  1685.         }
  1686.     }
  1687.  
  1688.     if (grid) {
  1689.         crv_count++;
  1690.         icrvs = icrvs->next;
  1691.         is_boundary = crv_count == grid_iso - 1 ||
  1692.               crv_count == grid_iso ||
  1693.               (icrvs && icrvs->next == NULL);
  1694.     }
  1695.     else {
  1696.         switch (crv_count++) {
  1697.         case 0:
  1698.             for (i = 0; i < grid_iso - 1; i++)
  1699.             icrvs = icrvs->next;
  1700.             break;
  1701.         case 1:
  1702.             icrvs = icrvs->next;
  1703.             break;
  1704.         case 2:
  1705.             while (icrvs->next)
  1706.             icrvs = icrvs->next;
  1707.             break;
  1708.         case 3:
  1709.             icrvs = NULL;
  1710.             break;
  1711.         }
  1712.         }
  1713.     }
  1714. }
  1715.  
  1716. /* Draw the bottom grid that hold the tic marks for 3d surface. */
  1717. static draw_bottom_grid(plot, min_z, max_z)
  1718.     struct surface_points *plot;
  1719.     double min_z, max_z;
  1720. {
  1721.     int i,j,k;                /* point index */
  1722.     int x,y,min_x = 10000,min_y = 10000;/* point in terminal coordinates */
  1723.     double xtic,ytic,ztic;
  1724.     struct termentry *t = &term_tbl[term];
  1725.  
  1726.     xtic = make_3dtics(x_min3d,x_max3d,'x',log_x);
  1727.     ytic = make_3dtics(y_min3d,y_max3d,'y',log_y);
  1728.     ztic = make_3dtics(min_z,max_z,'z',log_z);
  1729.  
  1730.     if (draw_border)
  1731.     if (parametric || !plot->has_grid_topology)
  1732.         draw_parametric_grid(plot);
  1733.     else
  1734.         draw_non_param_grid(plot);
  1735.  
  1736.     setlinestyle(-2); /* border linetype */
  1737.  
  1738. /* label x axis tics */
  1739.     if (xtics && xtic > 0.0) {
  1740.         switch (xticdef.type) {
  1741.             case TIC_COMPUTED:
  1742.          if (x_min3d < x_max3d)
  1743.             draw_3dxtics(xtic * floor(x_min3d/xtic),
  1744.                      xtic,
  1745.                      xtic * ceil(x_max3d/xtic),
  1746.                      min_sy_oy);
  1747.                 else
  1748.             draw_3dxtics(xtic * floor(x_max3d/xtic),
  1749.                      xtic,
  1750.                      xtic * ceil(x_min3d/xtic),
  1751.                      min_sy_oy);
  1752.             break;
  1753.         case TIC_SERIES:
  1754.         draw_series_3dxtics(xticdef.def.series.start, 
  1755.                     xticdef.def.series.incr, 
  1756.                     xticdef.def.series.end,
  1757.                     min_sy_oy);
  1758.         break;
  1759.         case TIC_USER:
  1760.         draw_set_3dxtics(xticdef.def.user,
  1761.                  min_sy_oy);
  1762.         break;
  1763.             default:
  1764.             (*t->text)();
  1765.             (void) fflush(outfile);
  1766.             int_error("unknown tic type in xticdef in do_3dplot", NO_CARET);
  1767.             break;        /* NOTREACHED */
  1768.         }
  1769.     }
  1770. /* label y axis tics */
  1771.     if (ytics && ytic > 0.0) {
  1772.         switch (yticdef.type) {
  1773.             case TIC_COMPUTED:
  1774.          if (y_min3d < y_max3d)
  1775.             draw_3dytics(ytic * floor(y_min3d/ytic),
  1776.                      ytic,
  1777.                      ytic * ceil(y_max3d/ytic),
  1778.                      min_sy_ox);
  1779.                 else
  1780.             draw_3dytics(ytic * floor(y_max3d/ytic),
  1781.                      ytic,
  1782.                      ytic * ceil(y_min3d/ytic),
  1783.                      min_sy_ox);
  1784.             break;
  1785.         case TIC_SERIES:
  1786.         draw_series_3dytics(yticdef.def.series.start, 
  1787.                     yticdef.def.series.incr, 
  1788.                     yticdef.def.series.end,
  1789.                     min_sy_ox);
  1790.         break;
  1791.         case TIC_USER:
  1792.         draw_set_3dytics(yticdef.def.user,
  1793.                  min_sy_ox);
  1794.         break;
  1795.             default:
  1796.             (*t->text)();
  1797.             (void) fflush(outfile);
  1798.             int_error("unknown tic type in yticdef in do_3dplot", NO_CARET);
  1799.             break;        /* NOTREACHED */
  1800.         }
  1801.     }
  1802. /* label z axis tics */
  1803.     if (ztics && ztic > 0.0 && (draw_surface ||
  1804.                 draw_contour == CONTOUR_SRF ||
  1805.                 draw_contour == CONTOUR_BOTH)) {
  1806.         switch (zticdef.type) {
  1807.             case TIC_COMPUTED:
  1808.          if (min_z < max_z)
  1809.             draw_3dztics(ztic * floor(min_z/ztic),
  1810.                      ztic,
  1811.                      ztic * ceil(max_z/ztic),
  1812.                  min_sx_ox,
  1813.                      min_sx_oy,
  1814.                      min_z,
  1815.                  max_z);
  1816.                 else
  1817.             draw_3dztics(ztic * floor(max_z/ztic),
  1818.                      ztic,
  1819.                      ztic * ceil(min_z/ztic),
  1820.                      min_sx_ox,
  1821.                  min_sx_oy,
  1822.                      max_z,
  1823.                  min_z);
  1824.             break;
  1825.         case TIC_SERIES:
  1826.         draw_series_3dztics(zticdef.def.series.start, 
  1827.                     zticdef.def.series.incr, 
  1828.                     zticdef.def.series.end,
  1829.                     min_sx_ox,
  1830.                     min_sx_oy,
  1831.                     min_z,
  1832.                     max_z);
  1833.  
  1834.         break;
  1835.         case TIC_USER:
  1836.         draw_set_3dztics(zticdef.def.user,
  1837.                  min_sx_ox,
  1838.                      min_sx_oy,
  1839.                      min_z,
  1840.                  max_z);
  1841.         break;
  1842.             default:
  1843.             (*t->text)();
  1844.             (void) fflush(outfile);
  1845.             int_error("unknown tic type in zticdef in do_3dplot", NO_CARET);
  1846.             break;        /* NOTREACHED */
  1847.         }
  1848.     }
  1849.  
  1850. /* PLACE XLABEL - along the middle grid X axis */
  1851.     if (strlen(xlabel) > 0) {
  1852.        int x1,y1;
  1853.        double step = apx_eq( min_sy_oy, y_min3d ) ?    (y_max3d-y_min3d)/4
  1854.                               : (y_min3d-y_max3d)/4;
  1855.            map3d_xy((x_min3d+x_max3d)/2,min_sy_oy-step, z_min3d,&x1,&y1);
  1856.        x1 += xlabel_xoffset * t->h_char;
  1857.        y1 += xlabel_yoffset * t->v_char;
  1858.        if ((*t->justify_text)(CENTRE))
  1859.         clip_put_text(x1,y1,xlabel);
  1860.        else
  1861.         clip_put_text(x1 - strlen(xlabel)*(t->h_char)/2,y1,xlabel);
  1862.     }
  1863.  
  1864. /* PLACE YLABEL - along the middle grid Y axis */
  1865.     if (strlen(ylabel) > 0) {
  1866.        int x1,y1;
  1867.        double step = apx_eq( min_sy_ox, x_min3d ) ?    (x_max3d-x_min3d)/4
  1868.                               : (x_min3d-x_max3d)/4;
  1869.            map3d_xy(min_sy_ox-step,(y_min3d+y_max3d)/2,z_min3d,&x1,&y1);
  1870.        x1 += ylabel_xoffset * t->h_char;
  1871.        y1 += ylabel_yoffset * t->v_char;
  1872.        if ((*t->justify_text)(CENTRE))
  1873.         clip_put_text(x1,y1,ylabel);
  1874.        else
  1875.         clip_put_text(x1 - strlen(ylabel)*(t->h_char)/2,y1,ylabel);
  1876.     }
  1877.  
  1878. /* PLACE ZLABEL - along the middle grid Z axis */
  1879.     if (strlen(zlabel) > 0 &&
  1880.         (draw_surface ||
  1881.      draw_contour == CONTOUR_SRF ||
  1882.      draw_contour == CONTOUR_BOTH)) {
  1883.            map3d_xy(min_sx_ox,min_sx_oy,max_z + (max_z-min_z)/4, &x, &y);
  1884.  
  1885.        x += zlabel_xoffset * t->h_char;
  1886.        y += zlabel_yoffset * t->v_char;
  1887.        if ((*t->justify_text)(CENTRE))
  1888.         clip_put_text(x,y,zlabel);
  1889.        else
  1890.         clip_put_text(x - strlen(zlabel)*(t->h_char)/2,y,zlabel);
  1891.     }
  1892. }
  1893.  
  1894. /* DRAW_3DXTICS: draw a regular tic series, x axis */
  1895. static draw_3dxtics(start, incr, end, ypos)
  1896.     double start, incr, end, ypos; /* tic series definition */
  1897.         /* assume start < end, incr > 0 */
  1898. {
  1899.     double ticplace;
  1900.     int ltic;        /* for mini log tics */
  1901.     double lticplace;    /* for mini log tics */
  1902.  
  1903.     end = end + SIGNIF*incr; 
  1904.  
  1905.     for (ticplace = start; ticplace <= end; ticplace +=incr) {
  1906.         if (ticplace < x_min3d || ticplace > x_max3d) continue;
  1907.         xtick(ticplace, xformat, incr, 1.0, ypos);
  1908.         if (log_x && incr == 1.0) {
  1909.             /* add mini-ticks to log scale ticmarks */
  1910.             for (ltic = 2; ltic <= 9; ltic++) {
  1911.                 lticplace = ticplace+log10((double)ltic);
  1912.                 xtick(lticplace, "\0", incr, 0.5, ypos);
  1913.             }
  1914.         }
  1915.     }
  1916. }
  1917.  
  1918. /* DRAW_3DYTICS: draw a regular tic series, y axis */
  1919. static draw_3dytics(start, incr, end, xpos)
  1920.     double start, incr, end, xpos; /* tic series definition */
  1921.         /* assume start < end, incr > 0 */
  1922. {
  1923.     double ticplace;
  1924.     int ltic;        /* for mini log tics */
  1925.     double lticplace;    /* for mini log tics */
  1926.  
  1927.     end = end + SIGNIF*incr; 
  1928.  
  1929.     for (ticplace = start; ticplace <= end; ticplace +=incr) {
  1930.         if (ticplace < y_min3d || ticplace > y_max3d) continue;
  1931.         ytick(ticplace, yformat, incr, 1.0, xpos);
  1932.         if (log_y && incr == 1.0) {
  1933.             /* add mini-ticks to log scale ticmarks */
  1934.             for (ltic = 2; ltic <= 9; ltic++) {
  1935.                 lticplace = ticplace+log10((double)ltic);
  1936.                 ytick(lticplace, "\0", incr, 0.5, xpos);
  1937.             }
  1938.         }
  1939.     }
  1940. }
  1941.  
  1942. /* DRAW_3DZTICS: draw a regular tic series, z axis */
  1943. static draw_3dztics(start, incr, end, xpos, ypos, z_min, z_max)
  1944.     double start, incr, end, xpos, ypos, z_min, z_max;
  1945.         /* assume start < end, incr > 0 */
  1946. {
  1947.     int x, y;
  1948.     double ticplace;
  1949.     int ltic;        /* for mini log tics */
  1950.     double lticplace;    /* for mini log tics */
  1951.     register struct termentry *t = &term_tbl[term];
  1952.  
  1953.     end = end + SIGNIF*incr; 
  1954.  
  1955.     for (ticplace = start; ticplace <= end; ticplace +=incr) {
  1956.         if (ticplace < z_min || ticplace > z_max) continue;
  1957.  
  1958.         ztick(ticplace, zformat, incr, 1.0, xpos, ypos);
  1959.         if (log_z && incr == 1.0) {
  1960.             /* add mini-ticks to log scale ticmarks */
  1961.             for (ltic = 2; ltic <= 9; ltic++) {
  1962.                 lticplace = ticplace+log10((double)ltic);
  1963.                 ztick(lticplace, "\0", incr, 0.5, xpos, ypos);
  1964.             }
  1965.         }
  1966.     }
  1967.  
  1968.     /* Make sure the vertical line is fully drawn. */
  1969.     setlinestyle(-2);    /* axis line type */
  1970.  
  1971.     map3d_xy(xpos, ypos, z_min3d, &x, &y);
  1972.     clip_move(x,y);
  1973.     map3d_xy(xpos, ypos, min(end,z_max)+(log_z ? incr : 0.0), &x, &y);
  1974.     clip_vector(x,y);
  1975.  
  1976.     setlinestyle(-1); /* border linetype */
  1977. }
  1978.  
  1979. /* DRAW_SERIES_3DXTICS: draw a user tic series, x axis */
  1980. static draw_series_3dxtics(start, incr, end, ypos)
  1981.         double start, incr, end, ypos; /* tic series definition */
  1982.         /* assume start < end, incr > 0 */
  1983. {
  1984.     double ticplace, place;
  1985.     double ticmin, ticmax;    /* for checking if tic is almost inrange */
  1986.     double spacing = log_x ? log10(incr) : incr;
  1987.  
  1988.     if (end == VERYLARGE)
  1989.         end = max(CheckLog(log_x, x_min3d), CheckLog(log_x, x_max3d));
  1990.     else
  1991.       /* limit to right side of plot */
  1992.       end = min(end, max(CheckLog(log_x, x_min3d), CheckLog(log_x, x_max3d)));
  1993.  
  1994.     /* to allow for rounding errors */
  1995.     ticmin = min(x_min3d,x_max3d) - SIGNIF*incr;
  1996.     ticmax = max(x_min3d,x_max3d) + SIGNIF*incr;
  1997.     end = end + SIGNIF*incr; 
  1998.  
  1999.     for (ticplace = start; ticplace <= end; ticplace +=incr) {
  2000.         place = (log_x ? log10(ticplace) : ticplace);
  2001.         if ( inrange(place,ticmin,ticmax) )
  2002.          xtick(place, xformat, spacing, 1.0, ypos);
  2003.     }
  2004. }
  2005.  
  2006. /* DRAW_SERIES_3DYTICS: draw a user tic series, y axis */
  2007. static draw_series_3dytics(start, incr, end, xpos)
  2008.         double start, incr, end, xpos; /* tic series definition */
  2009.         /* assume start < end, incr > 0 */
  2010. {
  2011.     double ticplace, place;
  2012.     double ticmin, ticmax;    /* for checking if tic is almost inrange */
  2013.     double spacing = log_y ? log10(incr) : incr;
  2014.  
  2015.     if (end == VERYLARGE)
  2016.         end = max(CheckLog(log_y, y_min3d), CheckLog(log_y, y_max3d));
  2017.     else
  2018.       /* limit to right side of plot */
  2019.       end = min(end, max(CheckLog(log_y, y_min3d), CheckLog(log_y, y_max3d)));
  2020.  
  2021.     /* to allow for rounding errors */
  2022.     ticmin = min(y_min3d,y_max3d) - SIGNIF*incr;
  2023.     ticmax = max(y_min3d,y_max3d) + SIGNIF*incr;
  2024.     end = end + SIGNIF*incr; 
  2025.  
  2026.     for (ticplace = start; ticplace <= end; ticplace +=incr) {
  2027.         place = (log_y ? log10(ticplace) : ticplace);
  2028.         if ( inrange(place,ticmin,ticmax) )
  2029.          ytick(place, xformat, spacing, 1.0, xpos);
  2030.     }
  2031. }
  2032.  
  2033. /* DRAW_SERIES_3DZTICS: draw a user tic series, z axis */
  2034. static draw_series_3dztics(start, incr, end, xpos, ypos, z_min, z_max)
  2035.         double start, incr, end; /* tic series definition */
  2036.         double xpos, ypos, z_min, z_max;
  2037.         /* assume start < end, incr > 0 */
  2038. {
  2039.     int x, y;
  2040.     double ticplace, place;
  2041.     double ticmin, ticmax;    /* for checking if tic is almost inrange */
  2042.     double spacing = log_x ? log10(incr) : incr;
  2043.     register struct termentry *t = &term_tbl[term];
  2044.  
  2045.     if (end == VERYLARGE)
  2046.         end = max(CheckLog(log_z, z_min), CheckLog(log_z, z_max));
  2047.     else
  2048.       /* limit to right side of plot */
  2049.       end = min(end, max(CheckLog(log_z, z_min), CheckLog(log_z, z_max)));
  2050.  
  2051.     /* to allow for rounding errors */
  2052.     ticmin = min(z_min,z_max) - SIGNIF*incr;
  2053.     ticmax = max(z_min,z_max) + SIGNIF*incr;
  2054.     end = end + SIGNIF*incr; 
  2055.  
  2056.     for (ticplace = start; ticplace <= end; ticplace +=incr) {
  2057.         place = (log_z ? log10(ticplace) : ticplace);
  2058.         if ( inrange(place,ticmin,ticmax) )
  2059.          ztick(place, zformat, spacing, 1.0, xpos, ypos);
  2060.     }
  2061.  
  2062.     /* Make sure the vertical line is fully drawn. */
  2063.     setlinestyle(-2);    /* axis line type */
  2064.  
  2065.     map3d_xy(xpos, ypos, z_min3d, &x, &y);
  2066.     clip_move(x,y);
  2067.     map3d_xy(xpos, ypos, min(end,z_max)+(log_z ? incr : 0.0), &x, &y);
  2068.     clip_vector(x,y);
  2069.  
  2070.     setlinestyle(-1); /* border linetype */
  2071. }
  2072.  
  2073. /* DRAW_SET_3DXTICS: draw a user tic set, x axis */
  2074. static draw_set_3dxtics(list, ypos)
  2075.     struct ticmark *list;    /* list of tic marks */
  2076.     double ypos;
  2077. {
  2078.     double ticplace;
  2079.     double incr = (x_max3d - x_min3d) / 10;
  2080.     /* global x_min3d, x_max3d, xscale, y_min3d, y_max3d, yscale */
  2081.  
  2082.     while (list != NULL) {
  2083.        ticplace = (log_x ? log10(list->position) : list->position);
  2084.        if ( inrange(ticplace, x_min3d, x_max3d)         /* in range */
  2085.           || NearlyEqual(ticplace, x_min3d, incr)    /* == x_min */
  2086.           || NearlyEqual(ticplace, x_max3d, incr))    /* == x_max */
  2087.         xtick(ticplace, list->label, incr, 1.0, ypos);
  2088.  
  2089.        list = list->next;
  2090.     }
  2091. }
  2092.  
  2093. /* DRAW_SET_3DYTICS: draw a user tic set, y axis */
  2094. static draw_set_3dytics(list, xpos)
  2095.     struct ticmark *list;    /* list of tic marks */
  2096.     double xpos;
  2097. {
  2098.     double ticplace;
  2099.     double incr = (y_max3d - y_min3d) / 10;
  2100.     /* global x_min3d, x_max3d, xscale, y_min3d, y_max3d, yscale */
  2101.  
  2102.     while (list != NULL) {
  2103.        ticplace = (log_y ? log10(list->position) : list->position);
  2104.        if ( inrange(ticplace, y_min3d, y_max3d)           /* in range */
  2105.           || NearlyEqual(ticplace, y_min3d, incr)    /* == y_min3d */
  2106.           || NearlyEqual(ticplace, y_max3d, incr))    /* == y_max3d */
  2107.         ytick(ticplace, list->label, incr, 1.0, xpos);
  2108.  
  2109.        list = list->next;
  2110.     }
  2111. }
  2112.  
  2113. /* DRAW_SET_3DZTICS: draw a user tic set, z axis */
  2114. static draw_set_3dztics(list, xpos, ypos, z_min, z_max)
  2115.     struct ticmark *list;    /* list of tic marks */
  2116.     double xpos, ypos, z_min, z_max;
  2117. {
  2118.     int x, y;
  2119.     double ticplace;
  2120.     double incr = (z_max - z_min) / 10;
  2121.     register struct termentry *t = &term_tbl[term];
  2122.  
  2123.     while (list != NULL) {
  2124.        ticplace = (log_z ? log10(list->position) : list->position);
  2125.        if ( inrange(ticplace, z_min, z_max)         /* in range */
  2126.           || NearlyEqual(ticplace, z_min, incr)        /* == z_min */
  2127.           || NearlyEqual(ticplace, z_max, incr))    /* == z_max */
  2128.         ztick(ticplace, list->label, incr, 1.0, xpos, ypos);
  2129.  
  2130.        list = list->next;
  2131.     }
  2132.  
  2133.     /* Make sure the vertical line is fully drawn. */
  2134.     setlinestyle(-2);    /* axis line type */
  2135.  
  2136.     map3d_xy(xpos, ypos, z_min, &x, &y);
  2137.     clip_move(x,y);
  2138.     map3d_xy(xpos, ypos, z_max+(log_z ? incr : 0.0), &x, &y);
  2139.     clip_vector(x,y);
  2140.  
  2141.     setlinestyle(-1); /* border linetype */
  2142. }
  2143.  
  2144. /* draw and label a x-axis ticmark */
  2145. static xtick(place, text, spacing, ticscale, ypos)
  2146.         double place;                   /* where on axis to put it */
  2147.         char *text;                     /* optional text label */
  2148.         double spacing;         /* something to use with checkzero */
  2149.         double ticscale;         /* scale factor for tic mark (0..1] */
  2150.     double ypos;
  2151. {
  2152.     register struct termentry *t = &term_tbl[term];
  2153.     char ticlabel[101];
  2154.     int x0,y0,x1,y1,x2,y2,x3,y3;
  2155.     int ticsize = (int)((t->h_tic) * ticscale);
  2156.     double v[2], len;
  2157.  
  2158.     place = CheckZero(place,spacing); /* to fix rounding error near zero */
  2159.  
  2160.     if (place > x_max3d || place < x_min3d) return;
  2161.  
  2162.     map3d_xy(place, ypos, z_min3d, &x0, &y0);
  2163.     /* need to figure out which is in. pick the middle point along the */
  2164.     /* axis as in.                               */
  2165.     map3d_xy(place, (y_max3d + y_min3d) / 2, z_min3d, &x1, &y1);
  2166.  
  2167.     /* compute a vector of length 1 into the grid: */
  2168.     v[0] = x1 - x0;
  2169.     v[1] = y1 - y0;
  2170.     len = sqrt(v[0] * v[0] + v[1] * v[1]);
  2171.     v[0] /= len;
  2172.     v[1] /= len;
  2173.  
  2174.     if (tic_in) {
  2175.     x1 = x0;
  2176.     y1 = y0;
  2177.     x2 = x1 + ((int) (v[0] * ticsize));
  2178.     y2 = y1 + ((int) (v[1] * ticsize));
  2179.         x3 = x0 - ((int) (v[0] * ticsize * 3)); /* compute text position */
  2180.         y3 = y0 - ((int) (v[1] * ticsize * 3));
  2181.     } else {
  2182.     x1 = x0;
  2183.     y1 = y0;
  2184.     x2 = x0 - ((int) (v[0] * ticsize));
  2185.     y2 = y0 - ((int) (v[1] * ticsize));
  2186.         x3 = x0 - ((int) (v[0] * ticsize * 4)); /* compute text position */
  2187.         y3 = y0 - ((int) (v[1] * ticsize * 4));
  2188.     }
  2189.     clip_move(x1,y1);
  2190.     clip_vector(x2,y2);
  2191.  
  2192.     /* label the ticmark */
  2193.     if (text == NULL)
  2194.      text = xformat;
  2195.  
  2196.     (void) sprintf(ticlabel, text, CheckLog(log_x, place));
  2197.     if (apx_eq(v[0], 0.0)) {
  2198.         if ((*t->justify_text)(CENTRE)) {
  2199.             clip_put_text(x3,y3,ticlabel);
  2200.         } else {
  2201.             clip_put_text(x3-(t->h_char)*strlen(ticlabel)/2,y3,ticlabel);
  2202.         }
  2203.     }
  2204.     else if (v[0] > 0) {
  2205.         if ((*t->justify_text)(RIGHT)) {
  2206.             clip_put_text(x3,y3,ticlabel);
  2207.         } else {
  2208.             clip_put_text(x3-(t->h_char)*strlen(ticlabel),y3,ticlabel);
  2209.         }
  2210.     } else {
  2211.         (*t->justify_text)(LEFT);
  2212.     clip_put_text(x3,y3,ticlabel);
  2213.     }
  2214. }
  2215.  
  2216. /* draw and label a y-axis ticmark */
  2217. static ytick(place, text, spacing, ticscale, xpos)
  2218.         double place;                   /* where on axis to put it */
  2219.         char *text;                     /* optional text label */
  2220.         double spacing;         /* something to use with checkzero */
  2221.         double ticscale;         /* scale factor for tic mark (0..1] */
  2222.     double xpos;
  2223. {
  2224.     register struct termentry *t = &term_tbl[term];
  2225.     char ticlabel[101];
  2226.     int x0,y0,x1,y1,x2,y2,x3,y3;
  2227.     int ticsize = (int)((t->h_tic) * ticscale);
  2228.     double v[2], len;
  2229.  
  2230.     place = CheckZero(place,spacing); /* to fix rounding error near zero */
  2231.  
  2232.     if (place > y_max3d || place < y_min3d) return;
  2233.  
  2234.     map3d_xy(xpos, place, z_min3d, &x0, &y0);
  2235.     /* need to figure out which is in. pick the middle point along the */
  2236.     /* axis as in.                               */
  2237.     map3d_xy((x_max3d + x_min3d) / 2, place, z_min3d, &x1, &y1);
  2238.  
  2239.     /* compute a vector of length 1 into the grid: */
  2240.     v[0] = x1 - x0;
  2241.     v[1] = y1 - y0;
  2242.     len = sqrt(v[0] * v[0] + v[1] * v[1]);
  2243.     v[0] /= len;
  2244.     v[1] /= len;
  2245.  
  2246.     if (tic_in) {
  2247.     x1 = x0;
  2248.     y1 = y0;
  2249.     x2 = x1 + ((int) (v[0] * ticsize));
  2250.     y2 = y1 + ((int) (v[1] * ticsize));
  2251.         x3 = x0 - ((int) (v[0] * ticsize * 3)); /* compute text position */
  2252.         y3 = y0 - ((int) (v[1] * ticsize * 3));
  2253.     } else {
  2254.     x1 = x0;
  2255.     y1 = y0;
  2256.     x2 = x0 - ((int) (v[0] * ticsize));
  2257.     y2 = y0 - ((int) (v[1] * ticsize));
  2258.         x3 = x0 - ((int) (v[0] * ticsize * 4)); /* compute text position */
  2259.         y3 = y0 - ((int) (v[1] * ticsize * 4));
  2260.     }
  2261.     clip_move(x1,y1);
  2262.     clip_vector(x2,y2);
  2263.  
  2264.     /* label the ticmark */
  2265.     if (text == NULL)
  2266.      text = yformat;
  2267.  
  2268.     (void) sprintf(ticlabel, text, CheckLog(log_y, place));
  2269.     if (apx_eq(v[0], 0.0)) {
  2270.         if ((*t->justify_text)(CENTRE)) {
  2271.             clip_put_text(x3,y3,ticlabel);
  2272.         } else {
  2273.             clip_put_text(x3-(t->h_char)*strlen(ticlabel)/2,y3,ticlabel);
  2274.         }
  2275.     }
  2276.     else if (v[0] > 0) {
  2277.         if ((*t->justify_text)(RIGHT)) {
  2278.             clip_put_text(x3,y3,ticlabel);
  2279.         } else {
  2280.             clip_put_text(x3-(t->h_char)*strlen(ticlabel),y3,ticlabel);
  2281.         }
  2282.     } else {
  2283.         (*t->justify_text)(LEFT);
  2284.     clip_put_text(x3,y3,ticlabel);
  2285.     }
  2286. }
  2287.  
  2288. /* draw and label a z-axis ticmark */
  2289. static ztick(place, text, spacing, ticscale, xpos, ypos)
  2290.         double place;                   /* where on axis to put it */
  2291.         char *text;                     /* optional text label */
  2292.         double spacing;         /* something to use with checkzero */
  2293.         double ticscale;         /* scale factor for tic mark (0..1] */
  2294.     double xpos, ypos;
  2295. {
  2296.     register struct termentry *t = &term_tbl[term];
  2297.     char ticlabel[101];
  2298.     int x0,y0,x1,y1,x2,y2,x3,y3;
  2299.     int ticsize = (int)((t->h_tic) * ticscale);
  2300.  
  2301.     place = CheckZero(place,spacing); /* to fix rounding error near zero */
  2302.  
  2303.     map3d_xy(xpos, ypos, place, &x0, &y0);
  2304.  
  2305.     if (tic_in) {
  2306.     x1 = x0;
  2307.     y1 = y0;
  2308.     x2 = x0 + ticsize;
  2309.     y2 = y0;
  2310.         x3 = x0 - ticsize;
  2311.         y3 = y0;
  2312.     } else {
  2313.     x1 = x0;
  2314.     y1 = y0;
  2315.     x2 = x0 - ticsize;
  2316.     y2 = y0;
  2317.         x3 = x0 - ticsize * 2; /* compute text position */
  2318.         y3 = y0;
  2319.     }
  2320.     clip_move(x1,y1);
  2321.     clip_vector(x2,y2);
  2322.  
  2323.     /* label the ticmark */
  2324.     if (text == NULL)
  2325.      text = zformat;
  2326.  
  2327.     (void) sprintf(ticlabel, text, CheckLog(log_z, place));
  2328.     if ((*t->justify_text)(RIGHT)) {
  2329.         clip_put_text(x3,y3,ticlabel);
  2330.     } else {
  2331.         clip_put_text(x3-(t->h_char)*(strlen(ticlabel)+1),y3,ticlabel);
  2332.     }
  2333. }
  2334.